package com.cmcc.pp.ui.platformmonitoring;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter.OnItemClickListener;
import com.cmcc.pp.R;
import com.cmcc.pp.base.BActivity;
import com.cmcc.pp.entity.NetworkElement;
import com.cmcc.pp.ui.platformmonitoring.adapter.NEAdapter;
import com.cmcc.pp.ui.platformmonitoring.adapter.RecentNEAdapter;
import com.zrspring.libv2.util.ToastUtils;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by ABC on 2018/1/2.
 * 选择网元
 */

public class ChoiceNetElementActivity extends BActivity {
    @Bind(R.id.activityChooseNE_tvNowNE)
    TextView mTvNowNe;
    @Bind(R.id.activityChooseNE_rvRecently)
    RecyclerView mRvRecently;
    @Bind(R.id.activityChooseNE_recyclerView)
    RecyclerView mRecyclerView;

    @Override
    protected boolean getHasTitle() {
        return true;
    }

    @Override
    public int getContentLayoutId() {
        return R.layout.activity_choicenetelement;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        getHeader().setTitle(R.string.choicenetelement);
        getHeader().showLeftText("取消");
        getHeader().setLeftTextColor(R.color.main_blue);

        mTvNowNe.setText(getString(R.string.ne_now, "Vert.x"));
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mRvRecently.setLayoutManager(new GridLayoutManager(this, 4, GridLayoutManager.VERTICAL, false));
    }

    @Override
    public void loadData() {
        final List<NetworkElement> recentNEs = DataSupport.findAll(NetworkElement.class);
        final RecentNEAdapter recentNEAdapter = new RecentNEAdapter(recentNEs);
        mRvRecently.setAdapter(recentNEAdapter);

        final ArrayList<NetworkElement> networkElements = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            NetworkElement networkElement = new NetworkElement();
            networkElement.name = "网元" + i;
            networkElements.add(networkElement);
        }
        NEAdapter neAdapter = new NEAdapter(networkElements);
        neAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                NetworkElement networkElement = networkElements.get(position);
                networkElement.save();
                recentNEs.add(networkElement);
                recentNEAdapter.notifyDataSetChanged();
                mTvNowNe.setText(getString(R.string.ne_now, networkElement.name));
                ToastUtils.show("点击了" + networkElement.name);
            }
        });
        mRecyclerView.setAdapter(neAdapter);
    }
}
